<?php
class omekpi_servicer_incremental{
    
    function getOrderIncremental($order = array()){ 
        $servicer_invoice = &app::get('omekpi')->model('servicer_invoice');//订单，人员关系表
        $ser_array= array();
        $addarray = array();
        $name = $this->getName($order['mark_text']);//帐户名称
        //$payed = $servicer_invoice->dump(array('order_id'=>$order['order_id']),'payed');

        $ser_array['createdate'] = date('Ymd',$order['createtime']);
        if($order['pay_status']==1 or $order['pay_status']==3 or $order['pay_status']==4){
            $ser_array['my_sales']  = $order['payed'];
            $ser_array['payed_orders'] = 1;
        }else{
            $ser_array['my_sales']  = 0;
            $ser_array['payed_orders'] = 0;
        }
        $ser_array['received_orders'] = 1;
        $employee = &app::get('omekpi')->model('employee');//员工列表
        $servicer_log = &app::get('omekpi')->model('servicer_log');//客服人员绩效表
        
        if($name){//没名字servicer_id = 0;
            $em_filter=array('employee_login_name'=>$name,'employee_type'=>'servicer');
            $r = $employee->getList('employee_id',$em_filter,0,-1);
            
            if(!$r[0]){
                return false;
            }else{
                $addarray['employee_id'] = $r[0]['employee_id'];
                $ser_array['employee_id'] = $r[0]['employee_id'];
                $res = $servicer_log->dump(array('employee_id'=>$addarray['employee_id'],'createdate'=>$ser_array['createdate']));
                $invoice = $servicer_invoice->dump(array('order_id'=>$order['order_id'],'employee_id'=>$addarray['employee_id']));
                if($invoice['payed']){
                    $ser_array['my_sales']=$ser_array['my_sales']-$invoice['payed'];
                }
                if($res){
	                $addarray['createdate'] = $ser_array['createdate'];
	                $addarray['my_sales'] = $ser_array['my_sales']+$res['my_sales'];
	                $addarray['payed_orders'] = $ser_array['payed_orders']+$res['payed_orders'];
	                $invoice_data = array('employee_id'=>$ser_array['employee_id'],'order_id'=>$order['order_id'],'createdate'=>$ser_array['createdate'],'payed'=>$order['payed']);
                //1：如果单子没有接过，第一次接，那么是插入操作
                //2：如果一个人单子接过第二次，但是和第一次的不是同一张单子，那么已接订单数要+1
                //3：如果一个人单子接过第二次，和第一次是同一张单子，只是第一次未付款，第二次执行付款操作，那么已接订单数不能+1
                //$invoice是通过order_id 和 employee_id 查询这个人是否接过这张单子的结果，如果有，说明此人已接过这单子，既已接订单数不能+1，反之要+1
			        if(empty($invoice)){
			        	$addarray['received_orders'] = $ser_array['received_orders']+$res['received_orders'];
				        
				        $servicer_invoice->insert($invoice_data);
			        }else{
			            $servicer_invoice->update(array('payed'=>$order['payed']),array('employee_id'=>$ser_array['employee_id'],'order_id'=>$order['order_id']));
			        }
			        
		            $servicer_log->update($addarray);
		        }else{
		        	$invoice_data = array('employee_id'=>$ser_array['employee_id'],'order_id'=>$order['order_id'],'createdate'=>$ser_array['createdate'],'payed'=>$order['payed']);
		        	if(empty($invoice)){
				        
				        $servicer_invoice->insert($invoice_data);
			        }else{
			        	$servicer_invoice->update(array('payed'=>$order['payed']),array('employee_id'=>$ser_array['employee_id'],'order_id'=>$order['order_id']));
			        }
		            $servicer_log->insert($ser_array);
		        }

            }
        }else{
            return false;
        }
        
    } 
    
    
    function getName($des){
        $o = $this->getCfg();
        if($o){
            $o[0] = preg_quote($o[0]);
            $o[1] = preg_quote($o[1]);
            $preg = "((?<=".$o[0].")([^".$o[0]."]*)(?=".$o[1]."))";
            preg_match($preg,$des,$name);
            return isset($name[0])?$name[0]:null;
        }else{
            return null;
        }
    }
    
    function getCfg(){
        $left = app::get('omekpi')->getConf('system.left_mark');
        $right = app::get('omekpi')->getConf('system.right_mark');
        if(empty($left) || empty($right)){
            app::get('omekpi')->setConf('system.left_mark','[-');
            app::get('omekpi')->setConf('system.right_mark','-]');
            return array(0=>'[-',1=>'-]');
        }else{
            return array(0=>"$left",1=>"$right");
        }
    }

}
?>
